package com.charactereducation.serviceproviderusercenter.controller;

import com.charactereducation.module.vo.User;
import com.charactereducation.serviceproviderusercenter.service.UserService;
import com.charactereducation.tools.def.Constant;
import com.charactereducation.tools.myRunTime.MyRunTimeExcption;
import com.charactereducation.tools.result.ReturnCode;
import com.charactereducation.webcore.controller.BaseController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-6-25.
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    @Autowired
    UserService userService;

    @GetMapping(value = "/{id}")
    public User get(@PathVariable("id") Long id) {
        logger.info("getUserById");
        User user = userService.getUserById(id);
        return user;
    }

    @PostMapping("/modify")
    public void modify(@RequestParam User user) {

        userService.modifyUser(user);


    }
    @GetMapping(value = "/pageInfo")
    public PageInfo pageInfo(@RequestParam Map<String, String> map) {
        logger.info("pageInfo===============>>>>>>>>>");
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);


        List<User> userList = userService.getUserList(map);

        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }
    @PostMapping(value = "/login")
    public User Login(@RequestParam String phone,@RequestParam String passWord) {
        User user = userService.Login(phone,passWord);
        if(user ==null){
            throw new MyRunTimeExcption(ReturnCode.USER_IS_INEXISTENCE);
        }
        return user;
    }


}
